package com.example.shouybl;

import android.app.Application;
import android.widget.Toast;

import com.example.shouybl.netstate.NetChangeObserver;
import com.example.shouybl.netstate.NetWorkUtil;
import com.example.shouybl.netstate.NetworkStateReceiver;
import com.example.shouybl.utils.ImageLoaderUtil;

/**
 * Created by Administrator on 2015/10/9.
 */
public class BaseApp extends Application {
    public static BaseApp app;

    private NetChangeObserver observer=new NetChangeObserver(){
        @Override
        public void onConnect(NetWorkUtil.NetType type) {
            super.onConnect(type);
            switch (type) {
                case WIFI:
                    showToast("WIFI已连接");
                    break;
                case GNET_3:
                    showToast("移动3G网络已连接");
                    break;
                default:
                    showToast("网络已连接");
                    break;
            }
        }

        @Override
        public void onDisConnect() {
            super.onDisConnect();
            showToast("网络不可用");
        }
    };

    public static BaseApp getInstance(){
        return app;
    }
    @Override
    public void onCreate() {
        super.onCreate();
        app=this;
        //初始化网络状态判断
        initNetState();

        //图片加载框架
        ImageLoaderUtil.configGolbalImageLoader(this);
    }

    private void initNetState() {
        NetworkStateReceiver.registerNetworkStateReceiver(this);
        NetworkStateReceiver.registerObserver(observer);
    }

    public void unegisterNetworkStateReceiver() {
        NetworkStateReceiver.removeRegisterObserver(observer);
        NetworkStateReceiver.unRegisterNetworkStateReceiver(this);
    }

    public void showToast(String msg) {
        Toast.makeText(BaseApp.this, msg, Toast.LENGTH_SHORT).show();
    }
}
